<?php
# @version SVN: $Id: tags.class.inc 590 2012-06-04 21:48:03Z novachok $

class tags {

   private static $_assignedTags = array();
   private static $_allTags      = NULL;

   public static function getAssignedTags($id)
   {
      if(empty($id) || !is_numeric($id)) return array();
      
      if(!isset(self::$_assignedTags[$id]))
      {
         $DB = database::getInstance();
         $DB->sql = 'SELECT `title` FROM `meccano_tags` AS `t1`, `meccano_tags2content` AS `t2` WHERE `t1`.`id` = `t2`.`tid` AND `t2`.`aid` = '. $id;
         self::$_assignedTags[$id] = $DB->DB_GetFlat();
      }

      return self::$_assignedTags[$id];
   }

   public static function setAssignedTags($id, $tags)
   {
      if(empty($id) || !is_numeric($id)) return FALSE;
      if(!empty($tags) && is_string($tags))
      {
         $tags = explode(',', $tags);
      }

      $FILTERS = new filters();
      $DB      = database::getInstance();

      $tmp = array();
      $tags = (array)$tags;
      if(!empty($tags))
      {
         for($i=0, $t=sizeof($tags); $i<$t; $i++)
         {
            $tg = mb_strtolower($FILTERS->ClearHtml($tags[$i]), 'UTF-8');
            $tg = trim($tg);
            if(!empty($tg)) $tmp[] = $tg;
         }
         $tags = $tmp;
      }

      # ADD NEW TAGS INTO DB
      if(!empty($tags))
      {
         $DB->sql = 'INSERT IGNORE INTO `meccano_tags` (`title`) VALUES ("'. implode('"),("', $tags) .'")';
         $DB->DB_Query();
      }

      # GET EXISTS TAGS IDs
      $tags_ids = array();
      $DB->sql = 'SELECT *
         FROM `meccano_tags`
         WHERE
            '. (!empty($tags) ? '`title` IN ("'. implode('","', $tags) .'") OR' : '') .'
            `id` IN (SELECT `tid` FROM `meccano_tags2content` WHERE `aid` = '. $id .')';
      $DB->DB_Fetch();
      for($i=0, $t=$DB->rows; $i<$t; $i++)
      {
         $row = $DB->fetched[$i];
         $tags_ids[$row['title']] = array(
            'id'      => $row['id']
         );
      }

      $tags_exist = self::getAssignedTags($id);
      $tags_exist = $tags_exist;
      $tags_added = empty($tags_exist) ? $tags : array_diff($tags, $tags_exist);
      $tags_remov = empty($tags_exist) ? false : array_diff($tags_exist, $tags);

      if(!empty($tags_ids))
      {
         # REMOVE TAGS;
         if(!empty($tags_remov))
         {
            $sql = array();
            foreach($tags_remov AS $tag)
            {
               $sql[] = $tags_ids[$tag]['id'];
            }

            if(!empty($sql))
            {
               # REMOVE DELETED TAGS
               $DB->sql = 'DELETE FROM `meccano_tags2content` WHERE `tid` IN ('. join(',', $sql) .') AND `aid` = '. $id;
               $DB->DB_Query();
            }
         }

         if(!empty($tags_added))
         {
            $sql = array();
            foreach($tags_added AS $tag)
            {
               $sql[] = $tags_ids[$tag]['id'];
            }

            $DB->sql = 'INSERT INTO `meccano_tags2content` (`tid`, `aid`) VALUES ('. join(', '. $id .'),(', $sql) .','. $id .')';
            $DB->DB_Query();
         }

         # REMOVE UNUSED TAGS
         $DB->sql = 'DELETE FROM `meccano_tags` WHERE `id` NOT IN (SELECT DISTINCT `tid` FROM `meccano_tags2content`)';
         $DB->DB_Query();

         if(isset(self::$_assignedTags[$id])) unset(self::$_assignedTags[$id]);
         if(!empty($tags_added) || !empty($tags_remov)) self::$_allTags = NULL;

         return TRUE;
      }

      return FALSE;
   }


   public static function getAllTags($trashold = 1)
   {
      if(self::$_allTags !== NULL)
         return self::$_allTags;
      if(!is_numeric($trashold)) $trashold = 1;

      $DB = database::getInstance();

      $sql = 'SELECT `id`, `title`, COUNT(*) AS `mention`
         FROM `meccano_tags`, `meccano_tags2content` 
         WHERE `meccano_tags`.`id` = `meccano_tags2content`.`tid` 
         GROUP BY `title`
         HAVING `mention` >= '. $trashold;
      $DB->sql = $sql;
      self::$_allTags = $DB->DB_Fetch();

      return self::$_allTags;
   }
}